<?php
/**
 * Created by PhpStorm.
 * User: Qch
 * Date: 2016/9/8
 * Time: 19:52
 */

namespace J\Storage;


use J\ICache;

class Memcached implements ICache
{
    /**
     * @var \Memcached
     */
    protected $cache;

    public function hasItem($key, $expire = null)
    {
        return $this->get($key) or ($this->cache->getResultCode() != \Memcached::RES_NOTFOUND);
    }

    /**
     * 清空缓存
     *
     */
    public function clear(){}

    /**
     * @param int|string $key
     * @param mixed $column
     * @return mixed
     */
    public function get($key, $column = null)
    {
        return $this->cache->get($key);
    }

    /**
     * 批量获取
     *
     * @param string[]|int[] $keys
     * @param string[] $column
     *
     * @return mixed
     */
    public function getMulti($keys, $column = null)
    {
        return $this->cache->getMulti($keys);
    }

    /**
     * 批量设置
     *   1: [key=>value]
     *   2: value, keys
     *
     * @param mixed $pairs 键值对/统一值
     * @param null $keys 键数组
     *
     * @return mixed
     */
    public function set($pairs, $keys = null)
    {
        if(null === $keys)
            return $this->cache->setMulti($pairs);
        return $this->cache->set($keys, $pairs);
    }

    /**
     * 删除
     *
     * @param mixed $keys
     *
     * @return mixed
     */
    public function delete($keys)
    {
        if(is_array($keys))
            return $this->cache->deleteMulti($keys);
        return $this->cache->delete($keys);
    }
}